package com.qianguo.buyer.tab;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.eva.androidlibrary.api.entity.UserInfo;
import com.qianguo.buyer.BuyerLoginActivity;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseFragment;
import com.qianguo.buyer.order.CancelOrderFragment_;
import com.qianguo.buyer.order.DoingOrderFragment_;
import com.qianguo.buyer.order.DoneOrderFragment_;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * TAB 买家订单
 * @author eva
 * @date 16/6/28
 */
@EFragment(R.layout.fragment_tab_buyer_order)
public class CopyBuyerOrderFragment extends BaseFragment {

    private static final int STATUS_DOING = 0;
    private static final int STATUS_FINISH = 1;
    private static final int STATUS_CANCEL = 2;

    public static String[] ORDER_STATUSES;

    @ViewById(R.id.order_tab_layout)
    TabLayout mTabLayout;
    @ViewById(R.id.order_view_pager)
    ViewPager mViewPager;

    @ViewById(R.id.buyer_order_layout)
    LinearLayout mOrderLayout;
    @ViewById(R.id.buyer_order_login_button)
    Button mBtnLogin;

    private OrderFragmentPagerAdapter mAdapter;

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initializeUI() {
        EventBus.getDefault().register(this);
        if (mApp.isLogin()) {
            initViewPager();
        } else {
            mOrderLayout.setVisibility(View.GONE);
        }
    }

    private void initViewPager() {
        mBtnLogin.setVisibility(View.GONE);
        mOrderLayout.setVisibility(View.VISIBLE);
        ORDER_STATUSES = getResources().getStringArray(R.array.order_statuses);

        mAdapter = new OrderFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                mTabLayout.setupWithViewPager(mViewPager);
            }
        });
    }

    @Click(R.id.buyer_order_login_button)
    void clickLogin() {
        BuyerLoginActivity.jumpToLogin(getActivity());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserInfo user) {
        initViewPager();
    }

    public class OrderFragmentPagerAdapter extends FragmentPagerAdapter {

        public OrderFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case STATUS_DOING:
                    f = new DoingOrderFragment_();
                    break;
                case STATUS_FINISH:
                    f = new DoneOrderFragment_();
                    break;
                case STATUS_CANCEL:
                    f = new CancelOrderFragment_();
                    break;
            }

            Bundle extras = new Bundle();
            extras.putInt("position", position);
            f.setArguments(extras);
            return f;
        }

        @Override
        public int getCount() {
            return ORDER_STATUSES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return ORDER_STATUSES[position];
        }
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == OrderDetailsActivity.ORDER_DETAILS_REQUEST) {
//            if (resultCode == OrderDetailsActivity.WEIGHT_FINISH) {
//                if (mViewPager.getCurrentItem() < STATUS_DOING) {
//                    mViewPager.setCurrentItem(STATUS_DOING);
//                }
//            }
//        }
//    }
}
